package com.dayouzi.crawler_monitor.controller;


import com.dayouzi.crawler_monitor.base.result.R;
import com.dayouzi.crawler_monitor.config.DataSource;
import com.dayouzi.crawler_monitor.pojo.page.PageBean;
import com.dayouzi.crawler_monitor.pojo.query.*;
import com.dayouzi.crawler_monitor.pojo.vo.CrawlerDataListVO;
import com.dayouzi.crawler_monitor.pojo.vo.CrawlerDataQueryVO;
import com.dayouzi.crawler_monitor.pojo.vo.CrawlerDataVO;
import com.dayouzi.crawler_monitor.pojo.vo.CrawlerPolicyVO;
import com.dayouzi.crawler_monitor.service.CrawlerDetailService;
import com.dayouzi.crawler_monitor.service.CrawlerPolicyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

@Api(description = "爬虫fix管理")
@RestController
@RequestMapping("/crawler/web")
@Slf4j
public class CrawlerDetailController {

    @Autowired
    private CrawlerDetailService crawlerDetailService;
    
    @Autowired
    private CrawlerPolicyService crawlerPolicyService;

    @ApiOperation("获取数据信息")
    @PostMapping("/crawlerDataById")
    public R getCrawlerDataById(
            @ApiParam(value = "数据data查询对象", required = true) @RequestBody CrawlerDataQuery crawlerDataQuery
    ) {
        ArrayList list = new ArrayList<>();
        //判断爬虫类型
        Integer spiderType = crawlerDataQuery.getSpiderType();
        if (spiderType == 0) {
            //国内招中标爬虫
            CrawlerDataVO crawlerDataVO = crawlerDetailService.getCrawlerDataById(crawlerDataQuery);
            list.add(crawlerDataVO);
        } else if (spiderType == 5) {
            //政策新闻爬虫
            CrawlerPolicyVO crawlerPolicyVO = crawlerPolicyService.getCrawlerDataById(crawlerDataQuery);
            list.add(crawlerPolicyVO);
        }
        return R.ok().data("result", list);
    }

    @ApiOperation("修改数据信息")
    @PostMapping("updateCommit")
    public R updateCommit(
            @ApiParam(value = "数据data信息", required = true) @Valid @RequestBody CrawlerDataUpdateVO crawlerDataUpdateVO
    ) {
        crawlerDetailService.updateCommit(crawlerDataUpdateVO);
        return R.ok();
    }

    @ApiOperation("修改数据状态")
    @GetMapping("updateStatus")
    public R updateStatus(
            @ApiParam(value = "状态", required = true) @RequestParam("status") Integer status,
            @ApiParam(value = "主键", required = true) @RequestParam("did") Integer did
    ) {
        crawlerDetailService.updateStatus(did, status);
        return R.ok();
    }

    @ApiOperation("修改数据正文")
    @PostMapping("updateInfo")
    public R updateInfo(
            @ApiParam(value = "dataId", required = true) @RequestParam("dataId") Integer id,
            @ApiParam(value = "tableName", required = true) @RequestParam("tableName") String tableName,
            @ApiParam(value = "info", required = true) @RequestParam String info
    ) {
        crawlerDetailService.updateInfo(id, tableName, info);
        return R.ok();
    }

    @ApiOperation("上传附件")
    @PostMapping("uploadFiles")
    public R uploadFile(
            @ApiParam(value = "id", required = true) @RequestParam("id") Integer id,
            @ApiParam(value = "tableName", required = true) @RequestParam("tableName") String tableName,
            @ApiParam(value = "files", required = true) @RequestParam("files") MultipartFile[] files
    ) {
        crawlerDetailService.uploadFile(id, tableName, files);
        return R.ok().message("上传成功");
    }

    @ApiOperation("新增招中标数据")
    @PostMapping("addData")
    public R addData(
            @ApiParam(value = "数据data", required = true) @Valid @RequestBody AddDataQuery addDataQuery
    ) {
        CrawlerDataQueryVO crawlerDataQueryVO = crawlerDetailService.addData(addDataQuery);
        return R.ok().data("result", crawlerDataQueryVO);
    }

    @ApiOperation("新增政策数据")
    @PostMapping("addDataPolicy")
    public R addDataPolicy(
            @ApiParam(value = "数据data", required = true) @Valid @RequestBody AddDataPolicyQuery addDataPolicyQuery
    ) {
        CrawlerDataQueryVO crawlerDataQueryVO = crawlerDetailService.addDataPolicy(addDataPolicyQuery);
        return R.ok().data("result", crawlerDataQueryVO);
    }

    @ApiOperation("查看最近爬取数据")
    @PostMapping("listPageCrawlerData")
    public R listPageCrawlerData(
            @ApiParam(value = "数据data查询对象", required = true) @RequestBody CrawlerDataListQuery crawlerDataListQuery
            ) {
        PageBean<CrawlerDataListVO> result = crawlerDetailService.listPageCrawlerData(crawlerDataListQuery);
        return R.ok().data("result", result);
    }

}
